﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using Model;

namespace SqlServerDAL
{
    public class NewsInfoDAL
    {
        #region Singleton
        static NewsInfoDAL _Instance;
        public static NewsInfoDAL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new NewsInfoDAL();
                }
                return _Instance;
            }
        }
        #endregion

        public List<NewsInfoType> GetPagedNewsInfoList(int pageSize, int pageIndex, string orderStr, out int count)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Constants.ConnStr,
                Constants.NewsInfoSP_GetPagedNewsInfoList,
                pageSize,
                pageIndex);

            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }
            dr = ds.Tables[1].CreateDataReader();

            return CBO.FillCollection<NewsInfoType>(dr);
        }

        public bool AddNewsInfo(NewsInfoType newsInfo)
        {
            return SqlHelper.ExecuteNonQuery(Constants.ConnStr,
                Constants.NewsInfoSP_AddNewsInfo, 
                newsInfo.Title,
                newsInfo.Summary,
                newsInfo.Body,
                newsInfo.ImagePath,
                newsInfo.CreateTime,
                newsInfo.LastUpdateTime
                ) > 0;
        }
    }
}
